package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            Map map = new HashMap<>();
            map.put("status", Orders.COMPLETED);
            map.put("begin", beginTime);
            map.put("end", endTime);
            Double turnover = orderMapper.sunByMap(map);
            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
        }

        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        // 获取日期字符串
        ArrayList<LocalDate> dateList = new ArrayList<>();
        String dateListStr = getDateListStr(begin, end, dateList);
        // 创建两个集合存放新增用户列表
        ArrayList<Integer> newUserList = new ArrayList<>();
        ArrayList<Integer> totalUserList = new ArrayList<>();
        // 获取第一天之前的总数量
        Integer userTotalCount = userMapper.getTotalCountBeforeDate(begin);
        for (LocalDate date : dateList) {
            Integer newUserCount = userMapper.getNewUserByDate(date);
            newUserList.add(newUserCount);
            // 获取当天用户总量 = 当前之前的总量 + 当日新增的数量
            userTotalCount += newUserCount;
            totalUserList.add(userTotalCount);
        }
        String newUserListStr = StringUtils.join(newUserList, ",");
        // 获取用户总量列表字段
        String totalUserListStr = StringUtils.join(totalUserList, ",");
        // 封装成userReportVO 返回
        UserReportVO userReportVO = new UserReportVO();
        userReportVO.setDateList(dateListStr);
        userReportVO.setNewUserList(newUserListStr);
        userReportVO.setTotalUserList(totalUserListStr);
        return userReportVO;
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> dateList = new ArrayList<>();
        String dateListStr = getDateListStr(begin, end, dateList);
        ArrayList<Integer> orderCountList = new ArrayList<>();
        ArrayList<Integer> validOrderCountList = new ArrayList<>();
        Integer totalCount = 0;
        Integer validTotalCount = 0;
        for (LocalDate date : dateList) {
            Integer orderCount = orderMapper.getCountByDateAndStatus(date, null);
            totalCount += orderCount;
            Integer validOrderCount = orderMapper.getCountByDateAndStatus(date, Orders.COMPLETED);
            validTotalCount += validOrderCount;
            orderCountList.add(orderCount);
            validOrderCountList.add(validOrderCount);
        }
        Double rate = 0.0;
        if (totalCount != 0) {
            rate = validTotalCount.doubleValue() / totalCount;
        }
        OrderReportVO orderReportVO = new OrderReportVO();
        orderReportVO.setOrderCompletionRate(rate);
        orderReportVO.setTotalOrderCount(totalCount);
        orderReportVO.setValidOrderCount(validTotalCount);
        orderReportVO.setOrderCountList(StringUtils.join(orderCountList, ",")); // 订单数量列表
        orderReportVO.setValidOrderCountList(StringUtils.join(validOrderCountList, ",")); // 有效订单数列表
        orderReportVO.setDateList(dateListStr);
        return orderReportVO;
    }

    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        // 1. 查询对应时间范围内每个商品以及对应的销量数据
        List<GoodsSalesDTO> salesDTOList = orderMapper.getSaleTop10(begin, end);

        // 2. 定义集合，将商品名称和对应的销量存储到集合中
        ArrayList<String> nameList = new ArrayList<>();
        ArrayList<Integer> numberList = new ArrayList<>();
        for (GoodsSalesDTO salesDTO : salesDTOList) {
            nameList.add(salesDTO.getName());
            numberList.add(salesDTO.getNumber());
        }

        // 3. 将上述两个集合转为字符串，封装到salesTop10ReportVO中，并返回
        SalesTop10ReportVO salesTop10ReportVO = new SalesTop10ReportVO();
        salesTop10ReportVO.setNumberList(StringUtils.join(numberList, ","));
        salesTop10ReportVO.setNameList(StringUtils.join(nameList, ","));
        return salesTop10ReportVO;
    }

    /**
     * 根据时间区间统计用户数量
     */
    public String getDateListStr(LocalDate begin, LocalDate end, ArrayList<LocalDate> dateList) {
        while (begin.compareTo(end) <= 0) {
            //1.3 将每一天都存储到集合中
            dateList.add(begin);
            begin = begin.plusDays(1);
        }
        // 1.4 将集合中的日期字符串以逗号分隔，转为一个字符串
        return StringUtils.join(dateList, ",");
    }
}
